#ifndef AVR8SA1OS1_H_
#define AVR8SA1OS1_H_

/*******************************************************************************
AVR8sa1os101.h

===============================================================================
Copyright (c) 2010, Centeye, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of Centeye, Inc. nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL CENTEYE, INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
===============================================================================

AVR8 standalone sensor operating system

This code supports basic IO functions for the 8-bit AVR sensor. When finalized this code
will generally be slow-moving except for major revisions or bug fixes. 

Origin:
Adapted from AtmelInterface by Travis Young, which was adapted from Craig's
comm handlers.

Change Log:
2010 May 21, Friday, birth
2010 August 6, Friday, documentation
Version 101:
2010 August 9, Monday, able to change I2C addresss

TO DO: 
1. Change the enumeration sequence
2. Make I2C address updatable in run-time
3. Implement reset?
******************************************************************************/
#include <avr/eeprom.h>

//Defines for the interface between AVR and vision chip NAVio12B module
#define PD_IO_0_5			0x3F
#define PD_IO_0_5_INV		0xC0
#define PD_IO7_HILO			0x80
#define PD_IO7_HILO_INV		0x7F
#define PB_CS1				0x0E
#define PB_CS1_INV			0xF1
#define PB_WNR				0x01
#define PB_WNR_INV			0xFE
#define PB_CLK				0xF0
#define PB_CLK_INV			0x0F

// SENSOR COMMANDS: 1 Byte header to define one of 256 different commands
//
// The command headers are defined in different header files, depending on
// the function of the command. In general:
// Commands 0-29: reserved for operating system and defined here
// Commands 30-59: reserved for basic vision chip (biases etc.) operation
// Commands 60 and up: reserved for the application.
//
// Below are commands 0-29 for the operating system
#define	COMM_CMD_RESET					0	// not yet implemented
#define	COMM_CMD_SET_ADDR				1
#define	COMM_CMD_ATT					2

// The following are to add support for SMBus interfaces that can't use raw I2C
// For more info: http://www.mjmwired.net/kernel/Documentation/i2c/smbus-protocol
#define COMM_CMD_SMBUS_READ				10	// Send a single byte in next read
#define COMM_CMD_SMBUS_READ_WORD		11	// Send a two-byte word, low-high
#define COMM_CMD_SMBUS_READ_BLOCK		12	// Send a length then the bytes
#define COMM_CMD_SMBUS_READ_CONTINUE	13	// Recognized a multi-transmission data set, wants more of it

unsigned char smbus_last_command;			// Since they're all two-part messages, save the state

// DATA SETS: Argument for "COMM_CMD_ATT" command to indicate what data
// set is set for transmit.
//
// The data sets are defined in different header files, depending on
// the function of the command. In general:
// Data sets 0-29: reserved for operating system
// Data set 30-59: reserved for basic vision chip operation
// Data set 60 and up: reserved for the application
//
// Below are data sets 0-29
#define	COMM_ATT_ENUM				0
#define	COMM_ATT_ENUM_DATA_SETS		1		// not implemented. Craig legacy

////////////////////////////////
// EEPROM VARIABLES
// These are the memory locations of the associated variables.
// The ATmega644P has 2kB EEPROM, so use addresses < 0x0800
// * 0x0000 is used in AVR8sa1os101.h
// For calibration values, each is 1 byte so use the top register to store the
// values, i.e. 0x0700 + the CMD number. They are defined in the respective
// files, this contains OS level variables.
#define 		EEP_I2C_address		0x0000

//////////////////////////////////////////////
// Transmit Variables
struct {
	unsigned char locked; 		
	unsigned char read_length;	
	unsigned char read[10];
} enumeration;

struct {
	unsigned char locked;
	unsigned char read_length;
	unsigned char read[2];
} last_cmd;

void atmel_init(void);

int process_commands(void);
unsigned char process_transmit(unsigned char array_to_transmit);
void os_init(void);

#endif /*AVR8SA1OS1_H_*/
